import { Controller, Post, Body, Delete, Param, Get, Put, BadRequestException } from '@nestjs/common';
import { CatalogService } from './catalog.service';
import { ApiBearerAuth, ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { CreateCatalogDto, CatalogResponseDto, DeleteCatalogDto, UpdateCatalogDto } from './dto';
import { Public } from '../auth/decorators/public.decorator';

@ApiTags(ModuleSwaggerTags.CATALOG)
@ApiBearerAuth()
@Controller('cms/catalog')
export class CatalogController {
  constructor(private readonly catalogService: CatalogService) {}

  @Post()
  @ApiOperation({ summary: '创建目录', description: '创建新的目录节点，支持设置父级目录' })
  @ApiResponse({
    status: 201,
    description: '目录创建成功',
    type: CatalogResponseDto
  })
  @ApiResponse({
    status: 400,
    description: '请求参数错误'
  })
  @ApiResponse({
    status: 404,
    description: '父级目录不存在'
  })
  async create(@Body() createCatalogDto: CreateCatalogDto): Promise<CatalogResponseDto> {
    const catalog = await this.catalogService.create(createCatalogDto);
    return new CatalogResponseDto(catalog);
  }

  @Put()
  @ApiOperation({ summary: '更新目录', description: '更新目录信息，编辑时必须有id' })
  @ApiResponse({
    status: 200,
    description: '目录更新成功',
    type: CatalogResponseDto
  })
  @ApiResponse({
    status: 400,
    description: '请求参数错误'
  })
  @ApiResponse({
    status: 404,
    description: '目录不存在'
  })
  async update(@Body() updateCatalogDto: UpdateCatalogDto): Promise<CatalogResponseDto> {
    const catalog = await this.catalogService.update(updateCatalogDto);
    return new CatalogResponseDto(catalog);
  }

  @Get('tree/:categoryId')
  @ApiOperation({ summary: 'CMS查询目录树', description: '获取指定分类的目录树结构，用于后台管理' })
  @ApiResponse({
    status: 200,
    description: '查询成功',
    type: [CatalogResponseDto]
  })
  @ApiResponse({
    status: 400,
    description: '请求参数错误'
  })
  async getTreeForCms(@Param('categoryId') categoryId: string) {
    const categoryIdNum = parseInt(categoryId, 10);
    if (isNaN(categoryIdNum)) {
      throw new BadRequestException('categoryId必须是有效的数字');
    }
    return await this.catalogService.findTreeForCms(categoryIdNum);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除目录节点', description: '删除指定的目录节点，如果有子节点会递归删除所有子节点' })
  @ApiResponse({
    status: 200,
    description: '删除成功',
    schema: {
      type: 'object',
      properties: {
        success: { type: 'boolean', description: '删除是否成功' },
        message: { type: 'string', description: '删除结果消息' },
        deletedCount: { type: 'number', description: '删除的节点数量' }
      }
    }
  })
  @ApiResponse({
    status: 400,
    description: '请求参数错误或删除失败'
  })
  @ApiResponse({
    status: 404,
    description: '目录节点不存在'
  })
  async delete(@Param('id') id: string): Promise<{ success: boolean; message: string; deletedCount: number }> {
    const nodeId = parseInt(id, 10);
    if (isNaN(nodeId)) {
      throw new BadRequestException('节点ID必须是有效的数字');
    }
    return await this.catalogService.delete(nodeId);
  }
}
